package com.example.demo;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


public class TestDemo {
    @Test
    public void testGet() throws ParseException, IOException {
        String url = "https://api.apiopen.top/getJoke?page=1&count=2&type=video";
        //链式调用
        HttpResponse response = new HttpRequest(url)
                .setHeaders(null)
                .setParams(null)
                .doGet();
        //输出响应
        System.out.println(EntityUtils.toString(response.getEntity()));
        //验证响应码
        int code = response.getStatusLine().getStatusCode();
        Assert.assertEquals(200, code);
    }

    @Test
    public void testPost() throws ParseException, IOException {
        String url = "http://www.51xiaogang.com/img/test.do";
        //header
        Map<String, Object> header = new HashMap<String, Object>();
        header.put("Content-Type","application/json");

        HttpResponse response = new HttpRequest(url)
                .setHeaders(header)
                .setContent("{\"id\":12,\"data\":\"restful api test\",\"version\":\"v2\",\"date\":\"20171217\"}")
                .doPost();
        //输出响应
        System.out.println(EntityUtils.toString(response.getEntity()));
        //验证响应码
        int code = response.getStatusLine().getStatusCode();
        Assert.assertEquals(200, code);
    }

    @Test
    public void test2() throws IOException, java.text.ParseException {
        String url = "https://api.apiopen.top/getJoke?page=1&count=2&type=video";
        HttpResponse response = new HttpRequest(url).doPost();

        String content = EntityUtils.toString(response.getEntity());
        int code = response.getStatusLine().getStatusCode();

        //输出响应
        System.out.println("code: " + code + "\ncontent: " + content);

        Map<String,Object> actualMap = ResponseChecker.parse(content, null);

        System.out.println(actualMap);


        Assert.assertEquals(200, actualMap.get("code"));
        Assert.assertEquals("成功!", actualMap.get("message"));
        System.out.println( actualMap.get("result[1].text"));
        Assert.assertEquals("没有对比就没有伤害，别人跳舞VS你跳舞，这伤害值不是一般的大！", actualMap.get("result[0].text"));

    }

    @Test
    public void test3() throws IOException, java.text.ParseException {
        String url = "https://api.apiopen.top/getJoke?page=1&count=2&type=video";
        HttpResponse response = new HttpRequest(url).doPost();

        String content = EntityUtils.toString(response.getEntity());
        int code = response.getStatusLine().getStatusCode();

        //输出响应
        System.out.println("code: " + code + "\ncontent: " + content);

        Map<String,Object> actualMap = ResponseChecker.parse(content, null);

        System.out.println(actualMap);

        response = new HttpRequest(url).doPost();
        ResponseChecker responseChecker = new ResponseChecker(response);
        responseChecker.codeCheck(200);
        responseChecker.dataCheck(actualMap);

    }


    @Test
    public void test4() throws IOException, java.text.ParseException {
        String url = "http://www.51xiaogang.com/img/test.do";
        HttpResponse response = new HttpRequest(url).doPost();

        String content = EntityUtils.toString(response.getEntity());
        int code = response.getStatusLine().getStatusCode();

        //输出响应
        System.out.println("code: " + code + "\ncontent: " + content);

        Map<String,Object> actualMap = ResponseChecker.parse(content, null);

        System.out.println(actualMap);
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");// a为am/pm的标记
        String date = new SimpleDateFormat("yyyy-M-dd HH:mm:ss").format(System.currentTimeMillis());// 获取当前时间


        Assert.assertEquals(true, actualMap.get("success"));
        Assert.assertEquals("request请求数据", actualMap.get("state"));
        Assert.assertEquals(date, actualMap.get("date"));

    }




}
